userRange = ngx.shared.userRange;
userIds = ngx.shared.userIds;
userRange:flush_all()
userIds:flush_all()

-- user uids  指向新服务的uid ,逗号分隔
userIds:set("new","")

-- user netrange  需要指向新服务器组 
local net_array={'10.1.1.0/24','10.1.2.0/24'}

-- 把10进制转为2进制
function byte2bin(n)
local t = {}
if n == 0 or n == "0" 
then
return "00000000"
end
for i=7,0,-1 do
t[#t+1] = math.floor(n / 2^i)
n = n % 2^i
end
return table.concat(t)
end

-- 把网段转为2进制的字符串
function netrange2bit(netrangestr)
local t = ""
local nett = strSplit(netrangestr,"/")
local netn1 = strSplit(nett[1],'.')
local netn2 = nett[2]
local result = {}
for i=1,4 do
	s=tostring(byte2bin(netn1[i]))
	if t == nil or t == ""
	then
	t=s
	else
	t=t..s
	end
	print("now is :"..t)
end
table.insert(result,netn2)
sub=string.sub(t,1,netn2)
table.insert(result,sub)
return result
end

-- 拆分字符串
function strSplit(str,delimiter)
if str == nil or str == '' or delimiter == nil then
	return nil 
end 
local delimiter1 = delimiter
if delimiter == '.' or delimiter == '+' or delimiter == '*' or delimiter == '-' or delimiter == '?' or delimiter == '%' then
	delimiter1 = "%"..delimiter --转义特殊字符的间隔符
end 
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter1) do
	table.insert(result, match)
end 
return result
end

for i=1,#net_array do
    netbit=netrange2bit(net_array[i]) 
    userRange:set('net'..tostring(i),netbit[1]..'/'..netbit[2])
end 

